﻿using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.Application.File.Dtos
{
    [DownloadFileInputValidation(ErrorMessage = "至少应该有一个字段不能为默认值")]
    public class DownloadFileInput
    {
        public long? Id { get; set; }
        public long[]? Ids { get; set; }
        public string? Url { get; set; }
        public string[]? Urls { get; set; }
    }

    public class DownloadFileInputValidationAttribute : ValidationAttribute
    {
        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value != null && value is DownloadFileInput)
            {
                var input = (DownloadFileInput)value;

                if (input.Id != null && input.Id != 0)
                {
                    return ValidationResult.Success;
                } else if (input.Ids != null && input.Ids.Length > 0)
                {
                    foreach (var item in input.Ids)
                    {
                        if (item == 0)
                        {
                            return new ValidationResult("id数组中不能存在默认值。");
                        }
                    }
                    return ValidationResult.Success;
                } else if (!string.IsNullOrWhiteSpace(input.Url))
                {
                    return ValidationResult.Success;
                } else if (input.Urls != null && input.Urls.Length > 0)
                {
                    foreach (var item in input.Urls)
                    {
                        if (string.IsNullOrWhiteSpace(item))
                        {
                            return new ValidationResult("url数组中不能存在默认值。");
                        }
                    }
                    return ValidationResult.Success;
                }
            }
            return new ValidationResult(ErrorMessage ?? "至少应该有一个字段不能为默认值。");
        }
    }
}